package com.bobexchen.nettywebsocket.handler;

import com.bobexchen.nettywebsocket.manager.NettyChannelManager;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@ChannelHandler.Sharable
public class NettyServerHandler extends ChannelInboundHandlerAdapter {

    private Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private NettyChannelManager channelManager;

    @Override
    public void channelActive(ChannelHandlerContext ctx) {
        //添加连接
        log.info("ChannelInboundHandlerAdapter客户端加入连接：" + ctx.channel());
        //从管理器中添加
        channelManager.add(ctx.channel());


        channelManager.addUser(ctx.channel(), ctx.channel().id().toString());
    }

    @Override
    public void channelUnregistered(ChannelHandlerContext ctx) {
        //从管理器中移除
        channelManager.remove(ctx.channel());
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        log.error("[exceptionCaught][连接({})发生异常]", ctx.channel().id(), cause);
        //断开连接
        ctx.channel().close();
    }
}
